package com.jeecms.wicool.action.product;

import static com.jeecms.common.page.SimplePage.cpn;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.product.ProductCate;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.product.ProductCateMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

/**
 * 商品类型Action
 */
@Controller
public class ProductCateAct {
	private static final Logger log = LoggerFactory.getLogger(ProductCateAct.class);

	@RequestMapping("/productCate/v_list.do")
	public String list(String cateName,Integer cateLevel,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(cateName,cateLevel,cpn(pageNo),
				CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		List<ProductCate> list = (List<ProductCate>) pagination.getList();
		for(ProductCate pc : list){
			ProductCate p = manager.findById(pc.getParentId());
			if(p!=null){
				pc.setParentName(p.getCateName());
			}else{
				pc.setParentName("基类");
			}
		}
		model.addAttribute("pagination", pagination);
		model.addAttribute("cateName", cateName);
		model.addAttribute("cateLevel", cateLevel);

		return "product/list_productCate";
	}
	
	@RequestMapping("/productCate/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ProductCate productCate = manager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<ProductCate> listProductCate=manager.getListByCateLevel(1,CmsUtils.getUser(request));
		ProductCate pc=new ProductCate();
		pc.setId(0);
		pc.setCateName("基类");
		listProductCate.add(pc);
		model.addAttribute("productCate",productCate);
		model.addAttribute("listShop",listShop);
		model.addAttribute("listProductCate",listProductCate);
		return "product/edit_productCate";
	}
	@RequestMapping("/productCate/o_save.do")
	public String save(ProductCate bean, Integer shopId,HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean.setOrderNum(org.apache.commons.lang.StringUtils.isEmpty(bean.getOrderNum()) ? "0" : bean.getOrderNum());
		bean = manager.save(bean,shopId);
		log.info("save ProductCate id={}", bean.getId());
		cmsLogMng.operating(request, "ProductCate.log.save",  "id=" + bean.getId());
		return "redirect:v_list.do";
	}
	@RequestMapping("/productCate/o_update.do")
	public String update(ProductCate bean,Integer shopId,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = simpleTime.format(new Date());
		Timestamp t = Timestamp.valueOf(dateTime);

		bean.setUpdateTime(t);
		bean=manager.update(bean,shopId);
		model.addAttribute("message", "global.success");
		log.info("update ProductCate success. id={}", bean.getId());
		cmsLogMng.operating(request, "ProductCate.log.update",  "id=" + bean.getId());
		return list(null,null,pageNo,request, model);
	}
	/**
	 * 添加商品类型
	 * @param model
	 * @return
	 */
	@RequestMapping("/productCate/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		ProductCate productCate = new ProductCate();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<ProductCate> listProductCate=manager.getListByCateLevel(1,CmsUtils.getUser(request));
		ProductCate pc = new ProductCate();
		pc.setId(0);
		pc.setCateName("基类");
		listProductCate.add(pc);
		model.addAttribute("productCate", productCate);
		model.addAttribute("listShop", listShop);
		model.addAttribute("listProductCate", listProductCate);
		return "product/add_productCate";
	}
	/**
	 * 添加商品类型子类
	 * @param model
	 * @return
	 */
	@RequestMapping("/productCate/v_addSon.do")
	public String addSon(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ProductCate productCateParent = manager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<ProductCate> listProductCate=manager.getListByCateLevel(1,CmsUtils.getUser(request));
		ProductCate productCate = new ProductCate();
		productCate.setParentId(productCateParent.getId());
		productCate.setParentName(productCateParent.getCateName());
		
		model.addAttribute("productCate", productCate);
		model.addAttribute("listProductCate", listProductCate);
		model.addAttribute("listShop", listShop);
		model.addAttribute("productCateParent", productCateParent);
		return "product/add_productCate_son";
	}
	
	///
	@RequestMapping("/productCate/o_delete.do")
	public String delete(Integer[] ids, String cateName,Integer cateLevel,Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ProductCate[] beans = manager.deleteByIds(ids);
		for (ProductCate bean : beans) {
			log.info("delete ProductCate id={}", bean.getId());
			cmsLogMng.operating(request, "ProductCate.log.delete", "id="
					+ bean.getId() + ";name=" + bean.getCateName());
		}
		return list(cateName,cateLevel,pageNo, request, model);
	}
	private WebErrors validateSave(ProductCate bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		ProductCate entity = manager.findById(id);
		if (errors.ifNotExist(entity, ProductCate.class, id)) {
			return true;
		}
		return false;
	}
	@Autowired
	private ProductCateMng manager;
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CmsLogMng cmsLogMng;
	@Autowired
	private DictionaryMng DicMng;
}
